<?php
	namespace Home\Model;
	use Think\Model;
	class UsersModel extends Model{
		
		public function addUsers(){
			$userInfo=$this->create();
			if(! $this->hasUsers(  $userInfo['user_name'] ) ){
				
				if( $this->add( $userInfo ) ){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
		
		public function  hasUsers( $userName ){
			$map['user_name']=$userName;
			return  $this->where( $map )->find();
		}
		//登陆验证
		public function loginUser(){
		    $userInfo['uname']=I('post.user_name')?I('post.user_name'):'';
		    $userInfo['password']=I('post.password')?I('post.password'):'';
                    $userList = $this->hasUser($userInfo['uname']);
			if( $userList ){
				if( $userInfo['password'] == $userList['pwd']){
					session( "user_name" , $userList['uname']);
					session( "user_password" , $userList['password']);
					session( "user_id" , $userList['id']);
					$data['status'] = 1;
				}else{
					$data['status']= 2;
				}
			}else{
				$data['status'] = 3;
			}
			return $data;
		}
		//查看 用户是否登陆
		public function  hasLogin (){
			$map['user_name'] =session("user_name");
			$map['user_password'] =session("user_password");
			return $this->where( $map )->select();
		}
		//检测用户是否存在
		public function hasUser($userName){
		    $where['uname']=$userName;
		    return $this->where($where)->find();
		}
		
		
		public function UsersReg(){
			$data['uname']=I("post.uname")?I("post.uname"):'';
			$data['pwd']=I("post.pwd")?I("post.pwd"):'';
			$data['email']="979928978@qq.com";
			if($this->add($data)){
				return true;
			}else{
				return false;
			}
		}
	}
?>